package com.xmall.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xmall.bean.vo.CategoryLevelVO;
import com.xmall.bean.vo.CategoryVO;
import com.xmall.bean.vo.ListDataVo;
import com.xmall.bean.table.*;
import com.xmall.mapper.BrandMapper;
import com.xmall.mapper.CategoryMapper;
import com.xmall.mapper.MallRegionMapper;
import com.xmall.service.MallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.System;
import java.util.Date;
import java.util.List;

@Service
public class MallServiceImpl implements MallService {

    @Autowired
    protected MallRegionMapper mallRegionMapper;
    @Autowired
    protected BrandMapper brandMapper;
    @Autowired
    protected CategoryMapper categoryMapper;

    @Override
    public List<RegionVO> administrativeRegion() {
        Integer i = 1;
        List<RegionVO> regionVOS = mallRegionMapper.selectByType(i);
        for (RegionVO regionVO : regionVOS) {
            List<ChildrenBeanX> children = regionVO.getChildren();
            for (ChildrenBeanX child : children) {
                int id = child.getId();
                List<ChildrenBean> childrenBeans = mallRegionMapper.selectById(id);
                child.setChildren(childrenBeans);
            }
        }
        return regionVOS;
    }

    @Override
    public ListDataVo showBrandList(Integer page, Integer limit, String sort, String order, Integer id, String name) {
        PageHelper.startPage(page, limit);
        BrandExample brandExample = new BrandExample();
        brandExample.setOrderByClause(sort + " " + order);
        BrandExample.Criteria criteria = brandExample.createCriteria();
        if (name != null && name != "") {
            criteria.andNameLike("%" + name + "%");
        }
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        criteria.andDeletedEqualTo(false);
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        ListDataVo<Brand> listDataVo = new ListDataVo<Brand>();
        listDataVo.setItems(brands);
        PageInfo<Brand> brandPageInfo = new PageInfo<>(brands);
        long total = brandPageInfo.getTotal();
        listDataVo.setTotal((int) total);
        return listDataVo;
    }


    @Override
    public Brand addBrand(Brand brand) {
        brand.setAddTime(new Date());
        brand.setUpdateTime(new Date());
        brand.setDeleted(false);
        brandMapper.insertSelective(brand);
        Brand brandVo = brandMapper.selectByPrimaryKey(brand.getId());
        // Brand brand1 = brandMapper.selectByBrand(brand);
        return brandVo;
    }

    @Override
    public Integer deleteService(Brand brand) {
        int i = brandMapper.deleteByPrimaryKey(brand.getId());
        return i ;
    }

    @Override
    public Integer updateBrand(Brand brand) {
        int i = brandMapper.updateByPrimaryKey(brand);
        return i;
    }

    @Override
    public List<CategoryVO> showList() {
        //商场管理-商品类目页面显示
        List<CategoryVO> categoryVOS = categoryMapper.selectByPid(0);
        return categoryVOS;
    }

    @Override
    public List<CategoryLevelVO> showListLevelOne() {
        List<CategoryLevelVO> categoryLevelVOS = categoryMapper.selectByLevel("L1");
        return categoryLevelVOS;
    }

    @Override
    //商品类目添加
    public Category updateCategory(Category category) {
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        category.setAddTime(date);
        category.setUpdateTime(date);
        //TODO：类目重名判断
        categoryMapper.insertSelective(category);

        return category;
    }

    @Override
    public Integer updateCategoryInMall(CategoryVO categoryVO) {
        if (!categoryVO.getLevel().equals("L1")){
            if(categoryVO.getPid() == null){
                return 401;
            }
        }
        categoryMapper.updateCategoryInMall(categoryVO);
        return 0;
    }

    @Override
    public void deleteCategoryByLogical(CategoryVO categoryVO) {
        Integer id = categoryVO.getId();
        categoryMapper.deleteCategoryById(id);
        categoryMapper.deleteCategoryByPid(id);
    }


}
